package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.config.param.Converse;
import com.sinog.biz.mapper.havexml.FileTemplateMapper;
import com.sinog.biz.service.business.FileTemplateService;
import com.sinog.biz.util.IdGen;
import com.sinog.core.domain.FileTemplate;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.PageReturnUtil;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @desc 文件模板实现类
 * @author sunsf
 * @date 2020-02-26-15:21
 */
@Service
public class FileTemplateServiceImpl extends ServiceImpl<FileTemplateMapper,FileTemplate> implements FileTemplateService {

    @Resource
    private FileTemplateMapper fileTemplateMapper;

    /**
     * 保存模板文件
     * @param text text
     * @return Result
     */
    @Override
    public Result saveData(String text) {
        FileTemplate fileTemplate = JsonUtil.toBean(text,FileTemplate.class);
        String id = fileTemplate.getId();
        if(null == id || StringUtils.equals(id,"")) {
            fileTemplate.setId(IdGen.strid());
        }
        boolean flag = fileTemplate.insertOrUpdate();
        if(flag) {
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 获取列表信息
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param key key
     * @return String
     */
    @Override
    public String getTemplateFileList(Integer pageIndex,Integer pageSize,
                                      @Converse(caseIf = "-1", converseTo = "0") String key) {
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        page = page.setRecords(fileTemplateMapper.getTemplateFileList(page,key));
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 获取列表信息
     * @param id id
     * @return Result
     */
    @Override
    public Result getTemplateInfoById(String id) {
        Map<String,Object> map = fileTemplateMapper.getTemplateInfoById(id);
        if(null != map && !map.isEmpty()) {
            return Result.success(map);
        }
        return Result.failure(ReturnCode.SELECT_FAILED);
    }

    /**
     * 删除模板文件
     * @param id id
     * @return Result
     */
    @Override
    public Result delTemplate(String id) {
        FileTemplate fileTemplate = new FileTemplate();
        FileTemplate template = fileTemplate.selectById(id);
        Map<String,String> map = new HashMap<>(16);
        if(null != template) {
            String chartpath = template.getChartpath();
            String tpath = template.getTpath();
            map.put("chartpath",chartpath);
            map.put("tpath",tpath);
        }
        boolean del = fileTemplate.deleteById(id);
        if(del) {
            return Result.success(map);
        }
        return Result.failure(ReturnCode.DELETE_FAILED);
    }
}